package com.linghang.jdbc;

import com.linghang.dao.ProductDao;
import com.linghang.entity.Product;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author qyyzxty@icloud.com
 * @data 2021/4/29
 **/
public class ProductDaoImpl implements ProductDao {

    @Override
    public List<Product> getAllProducts(Product product) throws SQLException {
        //sql语句
        String sql = "SELECT * FROM `product` where product_price > ? and product_category = ?";
        //参数
        List<String> list = new ArrayList<>();
        list.add(String.valueOf(5.0));
        list.add(String.valueOf(1));
        //返回的结果集
        ResultSet resultSet = JDBCUtil.executeQuery(sql, list);
        //返回结果
        List<Product> result = new ArrayList<>();
        //遍历结果集
        while (resultSet.next()) {
            Integer id = Integer.valueOf(resultSet.getString("id"));
            String name = resultSet.getString("product_name");
            Integer category = Integer.valueOf(resultSet.getString("product_category"));
            Double price = Double.valueOf(resultSet.getString("product_price"));
            //在返回列表添加数据
            System.out.println(new Product(id, name, category, price));
            result.add(new Product(id, name, category, price));
        }
        return result;
    }
}